package cn.com.backstage.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.*;
import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiT_member_entrepreneurService;
import cn.com.api.service.ApiT_member_partnerService;
import cn.com.api.service.ApiT_member_partner_taskService;
import cn.com.util.*;

/*
 * 企业家认证控制器
 */
@Controller
@RequestMapping(Utils.SYS_BACKSTAGE + "/")
public class T_memberentrepreneurController {

	private static Logger logger = Logger.getLogger(T_memberentrepreneurController.class);

	@Autowired
	private ApiT_member_entrepreneurService apiT_member_entrepreneurService;
	@Autowired
	private ApiT_member_partnerService apiT_member_partnerService;
	@Autowired
	private ApiT_member_partner_taskService apiT_member_partner_taskService;
	@Autowired
	private ApiT_memberService apiT_memberService;

	/**
	 * 跳转用户企业家认证信息界面
	 */
	@RequestMapping(value = "member_entrepreneur_detail")
	public ModelAndView member_entrepreneur_detail(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/member_entrepreneur_detail.jsp");
		String userid = request.getParameter("userid");
		Map map = new HashMap();
		map.put("user_id", userid);
		PageInfo page = new PageInfo();
		page.setCurrentPage(1);
		page.setShowCount(5);
		List<ApiT_member_entrepreneur> list = apiT_member_entrepreneurService.findT_member_entrepreneursByMap(map,
				page);
		mv.addObject("entrepreneurdetail", list.get(0));
		return mv;
	}

	/**
	 * 后台审核企业主认证
	 */
	@RequestMapping(value = "validate_member_entrepreneur")
	public void validate_member_partner(HttpServletRequest request, HttpServletResponse response) throws Exception {
		BaseObj baseObj = new BaseObj();
		try {
			Date now = new Date();
			ApiT_user_admin user = (ApiT_user_admin) request.getSession().getAttribute("user_admin");
			String id = request.getParameter("id");
			String reason = request.getParameter("reason");
			String status = request.getParameter("status");

			// 跟新申请资料表
			ApiT_member_entrepreneur apiT_member_entrepreneur = apiT_member_entrepreneurService
					.findT_member_entrepreneurById(Integer.valueOf(id));
			apiT_member_entrepreneur.setApply_uid(user.getUser_id() + "");
			apiT_member_entrepreneur.setUpdate_time(DateUtil.dateToString(now, "yyyy-MM-dd HH:mm:ss"));
			apiT_member_entrepreneur.setReason(reason);
			apiT_member_entrepreneur.setStatus(Integer.valueOf(status));
			apiT_member_entrepreneurService.updateT_member_entrepreneur(apiT_member_entrepreneur);

			// 跟新用户表状态
			ApiT_member apiT_member = apiT_memberService.findT_memberById(apiT_member_entrepreneur.getUser_id());
			if ("1".equals(status)) {// 通过
				apiT_member.setVerify_entrepreneur(1);
				if (apiT_member_entrepreneur.getReferee_id() != null
						&& !"".equals(apiT_member_entrepreneur.getReferee_id())) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.clear();
					map.put("partner_code", apiT_member_entrepreneur.getReferee_id());
					ApiT_member_partner apiT_member_partner = apiT_member_partnerService.findTmemberpartnersByMap(map);
					ApiT_member_partner_task apiT_member_partner_task = apiT_member_partner_taskService
							.findT_member_partner_taskById(Integer.valueOf(apiT_member_partner.getTask_id()));
					apiT_member_entrepreneur.setReferee_type(0);// 推荐
					apiT_member_entrepreneur.setBonus_ratio(apiT_member_partner.getBonus_ratio());
					Date dateAfter = DateUtil.getDateAfter(now, 1, apiT_member_partner_task.getSign_term());
					apiT_member_entrepreneur
							.setReferee_deadline(DateUtil.dateToString(dateAfter, "yyyy-MM-dd HH:mm:ss"));
				}
			} else {// 驳回
				apiT_member.setVerify_entrepreneur(3);
			}
			apiT_memberService.updateT_member(apiT_member);
			baseObj.handleSuccess();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			baseObj.handleException(e);
		}
		JsonUtil.printJson(response, JsonUtil.object2json(baseObj));
	}

	/**
	 * 企业主列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "member_entrepreneur-list")
	public ModelAndView member_entrepreneur_list(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_member_entrepreneur_list.jsp");

		Map<String, Object> conditions = new HashMap<String, Object>();
		List<Map<String, Object>> t_members = apiT_member_entrepreneurService.findMember_entrepreneursByMap(conditions);

		mv.addObject("member_entrepreneurs", t_members);
		//
		return mv;
	}

	/**
	 * 企业主列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "updateentrepreneurlogo")
	public void updateentrepreneurlogo(HttpServletRequest request, HttpServletResponse response, PageInfo pageInfo)
			throws Exception {
		BaseObj baseObj = new BaseObj();
		try {
			String entrepreneurid = request.getParameter("entrepreneurid");
			String logo_img = request.getParameter("logo_img");
			ApiT_member_entrepreneur apiT_member_entrepreneur = apiT_member_entrepreneurService
					.findT_member_entrepreneurById(Integer.valueOf(entrepreneurid));
			apiT_member_entrepreneur.setLogo_img(logo_img);
			apiT_member_entrepreneurService.updateT_member_entrepreneur(apiT_member_entrepreneur);
			baseObj.handleSuccess();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			baseObj.handleException(e);
		}
		JsonUtil.printJson(response, JsonUtil.object2json(baseObj));
	}

}
